<script lang="ts" setup>
import { ref, shallowRef } from 'vue';

import { useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';

import { useVbenForm } from '#/adapter/form';
import { saveOrganizationDepartmentApi } from '#/api';
import {
  OrganizationDepartmentTreeSelect,
  OrganizationMemberSelect,
  OrganizationSelect,
} from '#/components';

defineOptions({
  name: 'OrganizationDepartmentDrawer',
});

const managerSelectedOption = ref<{ label: string; value: string } | null>(
  null,
);
const organizationId = ref<any>(null);
const organizationOptions = ref<any[]>([]);

const [Form, formApi] = useVbenForm({
  schema: [
    {
      component: shallowRef(OrganizationSelect),
      componentProps: {
        class: 'mr-2, w-full',
        initOptions: organizationOptions,
        onSelect: (value: string, option: any) => {
          formApi.setFieldValue('organizationName', option.label);
        },
      },
      fieldName: 'organizationId',
      label: $t('org.organization.title'),
      rules: 'required',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'departmentName',
      label: $t('org.organizationDepartment.field.departmentName'),
      rules: 'required',
      description: '',
    },
    {
      component: shallowRef(OrganizationDepartmentTreeSelect),
      componentProps: {
        class: 'mr-2, w-full',
        onSelect: (value: any, option: any) => {
          formApi.setFieldValue('parentName', option.label);
        },
      },
      fieldName: 'parentId',
      label: $t('org.organizationDepartment.field.parentName'),
      rules: '',
      description: '',
    },
    {
      component: shallowRef(OrganizationMemberSelect),
      componentProps: {
        class: 'mr-2 w-1/2',
        organizationId,
        initOptions: managerSelectedOption,
        onSelect: (value: any, option: any) => {
          formApi.setFieldValue('managerName', option.label);
        },
      },
      fieldName: 'managerId',
      label: $t('org.organizationDepartment.field.managerName'),
      rules: '',
      description: '',
    },
  ],
  showDefaultActions: false,
  handleSubmit: onSubmit,
});

const [Drawer, drawerApi] = useVbenDrawer({
  onCancel() {
    drawerApi.close();
  },
  onConfirm: async () => {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    await formApi.submitForm();
    drawerApi.close();
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const { values, disabled } = drawerApi.getData<Record<string, any>>();
      if (values) {
        organizationId.value = values.organizationId;
        managerSelectedOption.value = values.managerId
          ? { label: values.managerName, value: values.managerId }
          : null;
        organizationOptions.value = values.organizationId
          ? [{ value: values.organizationId, label: values.organizationName }]
          : [];

        formApi.setValues(values);
      }
      const enableEdit = !!disabled;
      formApi.setState({ commonConfig: { disabled: enableEdit } });
      drawerApi.setState({ showConfirmButton: !enableEdit });
    }
  },
  title: $t('org.organizationDepartment.title'),
});

async function onSubmit(values: any) {
  const rawData = drawerApi.getData().values;
  values.departmentId = rawData.departmentId;

  await saveOrganizationDepartmentApi(values);
  drawerApi.setData({ success: true });
}
</script>
<template>
  <Drawer class="w-[600px]">
    <Form />
  </Drawer>
</template>
